﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TDT_Project.classes;
using System.Data;
namespace TDT_Project.Controller
{
    public partial class uc_tbk : System.Web.UI.UserControl
    {
        cls_lop lop = new cls_lop();
        cls_nienkhoa nk = new cls_nienkhoa();
        DataSet tkb = new DataSet();
        public string table = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["nk"] != null)
                    {
                        string nienkhoa = Session["nk"].ToString();
                        string hocky ="HK1";
                        string lop_id = Session["lop"].ToString();
                        load_thoikhoabieu(lop_id, hocky, nienkhoa);
                        DataSet ds = lop.Load_gvien_lop_quoc(nienkhoa, lop_id);
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            lb_thoikhobieu.Text = "THỜI KHÓA BIỂU LỚP " + ds.Tables[0].Rows[0]["TenLop"].ToString();
                            lb_gvien.Text = "<u>GVCN:</u> " + ds.Tables[0].Rows[0]["TenGiaoVien"].ToString();
                            lbl_NH.Text =  cls_dungchung.nienkhoa_ten_load;
                        }
                        DataSet ds_nk = nk.Get_TenTruong();
                        if (ds_nk != null && ds_nk.Tables[0].Rows.Count > 0)
                        {
                            lbl_truong.Text = ds_nk.Tables[0].Rows[0]["TenTruong"].ToString();
                           
                        }
                    }
                    else
                        Response.Redirect("Default.aspx");
                }
                catch (Exception)
                {

                    throw;
                }
            }

        }
        private void addRowgds_thoikhoabieu(string thu2, string thu3, string thu4, string thu5, string thu6, string thu7, string stiet, string buoi)
        {
            try
            {
                DataRow dr_1 = tkb.Tables[0].Rows.Add();
                dr_1["THU2"] = thu2.ToString();
                dr_1["THU3"] = thu3.ToString();
                dr_1["THU4"] = thu4.ToString();
                dr_1["THU5"] = thu5.ToString();
                dr_1["THU6"] = thu6.ToString();
                dr_1["THU7"] = thu7.ToString();
                dr_1["SOTIET"] = stiet.ToString();
                dr_1["BUOI"] = buoi.ToString();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
        private void load_thoikhoabieu(string lop_id, string hocky, string nienhoc)
        {
            DataSet dsInput = lop.Load_TKBieu_quoc(lop_id, hocky, nienhoc);
            if (dsInput != null && dsInput.Tables[0].Rows.Count > 0)
            {

                tkb = dsInput.Copy();
                tkb.Clear();
                int cc = 1, shl = 1; int l = 0;
                string thu2_1 = "", thu3_1 = "", thu4_1 = "", thu5_1 = "", thu6_1 = "", thu7_1 = "", tiet = "", buoi = "";
                DataColumn thu2 = new DataColumn("THU2");
                tkb.Tables[0].Columns.Add(thu2);
                DataColumn thu3 = new DataColumn("THU3");
                tkb.Tables[0].Columns.Add(thu3);
                DataColumn thu4 = new DataColumn("THU4");
                tkb.Tables[0].Columns.Add(thu4);
                DataColumn thu5 = new DataColumn("THU5");
                tkb.Tables[0].Columns.Add(thu5);
                DataColumn thu6 = new DataColumn("THU6");
                tkb.Tables[0].Columns.Add(thu6);
                DataColumn thu7 = new DataColumn("THU7");
                tkb.Tables[0].Columns.Add(thu7);
                DataColumn stiet = new DataColumn("SOTIET");
                tkb.Tables[0].Columns.Add(stiet);
                DataColumn buoi_sag = new DataColumn("BUOI");
                tkb.Tables[0].Columns.Add(buoi_sag);
                int k = 0;

                if (dsInput.Tables[0].Rows[0]["Buoi_id"].ToString() == "B02")
                {
                    k = 6;
                    buoi = "CHIỀU";
                }
                else
                {
                    k = 1;
                    buoi = "SÁNG";
                }

                if (k == 6)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        addRowgds_thoikhoabieu(thu2_1, thu3_1, thu4_1, thu5_1, thu6_1, thu7_1, (i + 1).ToString(), "SÁNG");
                    }
                    while (k <= 10)
                    {
                        DataTable dbPL = new DataTable();
                        dbPL = dsInput.Tables[0].Clone();
                        DataRow[] results1 = dsInput.Tables[0].Select("Tiet=" + k);
                        foreach (DataRow drn in results1)
                            dbPL.ImportRow(drn);
                        DataSet ds = new DataSet();
                        ds.Tables.Add(dbPL);
                        tiet = k.ToString();
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "2")
                            {
                                thu2_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (k == 9)
                            {
                                thu2_1 = "SHL";
                            }
                            if (k == 10)
                            {
                                thu2_1 = "Chào cờ";
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "3")
                            {
                                thu3_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "4")
                            {
                                thu4_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "5")
                            {
                                thu5_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "6")
                            {
                                thu6_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "7")
                            {
                                thu7_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                        }
                        addRowgds_thoikhoabieu(thu2_1, thu3_1, thu4_1, thu5_1, thu6_1, thu7_1, tiet, buoi);
                        thu2_1 = ""; thu3_1 = ""; thu4_1 = ""; thu5_1 = ""; thu6_1 = ""; thu7_1 = ""; tiet = "";
                        k = k + 1;
                    }
                }

                else
                {
                    while (k <= 5)
                    {
                        DataTable dbPL = new DataTable();
                        dbPL = dsInput.Tables[0].Clone();
                        DataRow[] results1 = dsInput.Tables[0].Select("Tiet=" + k);
                        foreach (DataRow drn in results1)
                            dbPL.ImportRow(drn);
                        DataSet ds = new DataSet();
                        ds.Tables.Add(dbPL);
                        tiet = k.ToString();
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "2")
                            {
                                thu2_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (k == 2)
                            {
                                thu2_1 = "SHL";
                            }
                            if (k == 1)
                            {
                                thu2_1 = "Chào cờ";
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "3")
                            {
                                thu3_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "4")
                            {
                                thu4_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "5")
                            {
                                thu5_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "6")
                            {
                                thu6_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                            if (ds.Tables[0].Rows[i]["THU"].ToString() == "7")
                            {
                                thu7_1 = ds.Tables[0].Rows[i]["TenMonHoc"].ToString();
                            }
                        }
                        addRowgds_thoikhoabieu(thu2_1, thu3_1, thu4_1, thu5_1, thu6_1, thu7_1, tiet, buoi);
                        thu2_1 = ""; thu3_1 = ""; thu4_1 = ""; thu5_1 = ""; thu6_1 = ""; thu7_1 = ""; tiet = "";
                        k = k + 1;
                    }
                    for (int i = 5; i < 10; i++)
                    {
                        addRowgds_thoikhoabieu(thu2_1, thu3_1, thu4_1, thu5_1, thu6_1, thu7_1, (i + 1).ToString(), "CHIỀU");
                    }
                }
                tkb.AcceptChanges();
                if (tkb.Tables[0].Rows.Count > 0 && tkb != null)
                {
                    gidview.DataSource = tkb.Tables[0].DefaultView;
                    gidview.DataBind();
                }
            }
        }

        protected void gidview_DataBound(object sender, EventArgs e)
        {
            for (int i = gidview.Rows.Count - 1; i > 0; i--)
            {
                GridViewRow row = gidview.Rows[i];
                GridViewRow previousRow = gidview.Rows[i - 1];

                if (row.Cells[0].Text == previousRow.Cells[0].Text)
                {
                    if (previousRow.Cells[0].RowSpan == 0)
                    {
                        if (row.Cells[0].RowSpan == 0)
                        {
                            previousRow.Cells[0].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[0].RowSpan = row.Cells[0].RowSpan + 1;
                        }
                        row.Cells[0].Visible = false;
                    }

                }
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Session["nk"] != null)
            {
                string nienkhoa = Session["nk"].ToString();              
                string lop_id = Session["lop"].ToString();
                if (RadioButtonList1.SelectedValue == "HK1")
                {
                  
                    load_thoikhoabieu(lop_id, "HK1", nienkhoa);
                }
                else
                {
                    load_thoikhoabieu(lop_id, "HK2", nienkhoa);
                }
            }
            else
                Response.Redirect("Default.aspx");
        }


    }
}